/*
 * @lc app=leetcode.cn id=1694 lang=typescript
 *
 * [1694] 重新格式化电话号码
 */

// @lc code=start
function reformatNumber(number: string): string {
    let set = "0123456789";
    const last = number.split("").filter((i) => {
        return set.includes(i);
    });
    let final = "";

    for (let i = 0; i < last.length; i++) {
        const element = last[i];
        final += element;
        if ((i + 1) % 3 === 0) {
            final += "-";
        }
    }
    if (last.length % 3 === 0) {
        return final.slice(0, final.length - 1);
    }
    if (last.length % 3 === 1) {
        return (
            final.slice(0, final.length - 3) +
            "-" +
            last.slice(last.length - 2, last.length).join("")
        );
    }
    return final;
}
// @lc code=end
